<?php

namespace App\Code\Controllers;

use App\Code\Repositories\CmQuestion;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class CmQuestionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmQuestion(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('subject_id');
            $grid->column('question_text');
//            $grid->column('answer')->width('10px');
            $grid->column('difficulty');
            $grid->column('status');
            $grid->column('problem_logo')->width('10px')->image();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableViewButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmQuestion(), function (Show $show) {
            $show->field('id');
            $show->field('subject_id');
            $show->field('question_text');
            //将answer 富文本内容回显
            $show->field('answer')->unescape()->as(function ($content) {
                // 回显![](https://codem-admin.wfcloud.online/uploads/markdown/images/47f5ce1eb27322cc0ee1a43625ed928566ab5971cadcb.jpg)
                $content = preg_replace('/!\[\]\((.*?)\)/', '<img src="$1" alt="">', $content);
                // div 标签包裹
                return '<div>' . $content . '</div>';
            });
//            $show->field('answer', function ($value) {
//                return Editor::make()->display($value);
//            });
            $show->field('difficulty');
            $show->field('status');
            // 显示logo
            $show->field('problem_logo')->image();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmQuestion(), function (Form $form) {
            $form->display('id');
//            $form->text('s_id');
            $form->text('question_text');
            $form->markdown('answer')->disk('admin')->height('1000');
//            $form->text('difficulty');
//            $form->text('status');
//            $form->image('problem_logo')->uniqueName()->autoUpload()->override()->removable(false)->saveFullUrl();
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
